package com.star.common.model;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author xiaoqiang
 */
@Data
public class BaseResponse {

//    @ApiModelProperty(value = "状态码")
    private Integer code;

//    @ApiModelProperty(value = "消息")
    private String message = "";

//    @ApiModelProperty(value = "数据")
    private Object data = new JSONObject();

    public BaseResponse message(String message){
        this.code = 200;
        this.message = message;
        return this;
    }

    public BaseResponse error(String message){
        this.code = 400;
        this.message = message;
        return this;
    }
    public BaseResponse authError(String message){
        this.code = 401;
        this.message = message;
        return this;
    }
    public BaseResponse notFound(String message){
        this.code = 404;
        this.message = message;
        return this;
    }
    public BaseResponse data(Object data){
        this.code = 200;
        this.data = data;
        return this;
    }

    public BaseResponse array(Object data){
        this.code = 200;
        Map<String, Object> result = new HashMap<>();
        result.put("array",data);
        this.data = result;
        return this;
    }

    public BaseResponse(Object data){
        this.code = 200;
        if(!Objects.isNull(data)){
            this.data = data;
        }
    }

    public BaseResponse(Boolean flag){
        if(flag){
            this.code = 200;
            this.message = "success";
        }else{
            this.code = 400;
            this.message = "fail";
        }
    }

    public BaseResponse(boolean flag){
        if(flag){
            this.code = 200;
            this.message = "success";
        }else{
            this.code = 400;
            this.message = "fail";
        }
    }

    public BaseResponse(){
        this.code = 200;
        this.message = "success";
    }

    public BaseResponse success(){
        this.code = 200;
        this.message = "success";
        return this;
    }



}
